/*
 * @Author: CQJ
 * @Date: 2022-09-08 01:22:11
 * @LastEditors: CQJ
 * @LastEditTime: 2022-09-08 01:36:23
 * @Description:
 */
import { reactive, ref } from 'vue'
export interface chaptersItem {
  article_content: string
  chapter_no: string
  chapter_name: string
  is_edit: boolean
}
interface IPersonTags {
  tag_id: string
  tag_name: string
}

export interface IFormState {
  person_name: string
  person_name_alias: string | undefined
  person_name_eng: string | undefined
  beanId: string | undefined
  person_categ_id: string | undefined
  area_id: string | undefined
  area_name: string | undefined
  person_gender: number | undefined
  person_blood_type: string | undefined
  person_constellation: string | undefined
  person_height: undefined
  person_weight: undefined
  person_nationality: string | undefined
  person_ethnic: string | undefined
  person_birthday: any
  person_birthplace: any
  person_profession: string | undefined
  person_brokerage: string | undefined
  person_weibo: string | undefined
  person_school: string | undefined
  person_main_work: string | undefined
  person_relation: string | undefined
  person_introduction: string | undefined
  person_cover_vertical: string | undefined
  person_cover_vertical_type: number
  person_avatar: string | undefined
  person_avatar_type: number
  person_detail: string | undefined
  person_status: number
  person_keyword: string | undefined
  person_tags: IPersonTags[] | undefined
  person_tags_copy: IPersonTags[] | undefined
  person_pop_compensated: number
  person_pop_add_star: number
  person_pop_add_total: number
  person_pop_add_day: number
  person_pop_add_week: number
  person_pop_add_month: number
  person_pop_add_view: number
  person_pop_add_like: number
  person_pop_add_dislike: number
  person_pop_star: number
  person_pop_total: number
  person_pop_day: number
  person_pop_week: number
  person_pop_month: number
  person_pop_view: number
  person_pop_like: number
  person_pop_dislike: number
  site_id: string | undefined
  person_id: string | undefined
}

// 状态管理
export const formState = reactive<IFormState>({
  person_name: '',
  person_name_alias: '',
  person_name_eng: '',
  beanId: '',
  person_categ_id: undefined,
  area_id: undefined,
  area_name: undefined,
  person_gender: undefined,
  person_blood_type: undefined,
  person_constellation: undefined,
  person_height: undefined,
  person_weight: undefined,
  person_nationality: '',
  person_ethnic: '',
  person_birthday: '',
  person_birthplace: '',
  person_profession: '',
  person_brokerage: '',
  person_weibo: '',
  person_school: '',
  person_main_work: '',
  person_relation: '',
  person_introduction: '',
  person_cover_vertical: '',
  person_cover_vertical_type: 1,
  person_avatar: '',
  person_avatar_type: 1,
  person_detail: '',
  person_status: 1,
  person_keyword: '',
  person_tags: undefined,
  person_tags_copy: undefined,
  person_pop_compensated: 2,
  person_pop_add_star: 0,
  person_pop_add_total: 0,
  person_pop_add_day: 0,
  person_pop_add_week: 0,
  person_pop_add_month: 0,
  person_pop_add_view: 0,
  person_pop_add_like: 0,
  person_pop_add_dislike: 0,
  person_pop_star: 0,
  person_pop_total: 0,
  person_pop_day: 0,
  person_pop_week: 0,
  person_pop_month: 0,
  person_pop_view: 0,
  person_pop_like: 0,
  person_pop_dislike: 0,
  site_id: undefined,
  person_id: undefined
})
export interface ClearExtendInfo {
  person_pop_add_total: number
  person_pop_add_day: number
  person_pop_add_week: number
  person_pop_add_month: number
  person_pop_add_view: number
  person_pop_add_like: number
  person_pop_add_dislike: number
  person_pop_add_star: number
  person_pop_total: number
  person_pop_day: number
  person_pop_week: number
  person_pop_month: number
  person_pop_view: number
  person_pop_like: number
  person_pop_dislike: number
}
//随机生成区间范围数
const selectFrom = (lowerValue: number, upperValue: number) => {
  let choices = upperValue - lowerValue + 1
  return Math.floor(Math.random() * choices + lowerValue)
}
//判断是否是小数
const isDot = (num: number) => {
  if (String(num).indexOf('.') > -1) {
    return true
  } else {
    return false
  }
}
//以0.5为一个step向上取整
const floorFunc = (num: number) => {
  if (isDot(num)) {
    const int = parseInt(num.toString())
    const floatNum = num - int
    if (floatNum < 0.5) {
      return int + 0.5
    } else {
      return int + 1
    }
  }
  return num
}
//重置扩展信息
export const resetExtendFunc = () => {
  const form = {
    person_pop_add_total: formState.person_pop_add_total,
    person_pop_add_day: formState.person_pop_add_day,
    person_pop_add_week: formState.person_pop_add_week,
    person_pop_add_month: formState.person_pop_add_month,
    person_pop_add_view: formState.person_pop_add_view,
    person_pop_add_like: formState.person_pop_add_like,
    person_pop_add_dislike: formState.person_pop_add_like,
    person_pop_add_star: formState.person_pop_add_star,
    person_pop_total: formState.person_pop_total,
    person_pop_day: formState.person_pop_day,
    person_pop_week: formState.person_pop_week,
    person_pop_month: formState.person_pop_month,
    person_pop_view: formState.person_pop_view,
    person_pop_like: formState.person_pop_like,
    person_pop_dislike: formState.person_pop_like
  }
  let key: keyof ClearExtendInfo
  for (key in form) {
    formState[key] = 0
  }
}
//随机补偿方法
export const randomFunc = () => {
  const data = {
    person_pop_add_total: 0,
    person_pop_add_day: 0,
    person_pop_add_week: 0,
    person_pop_add_month: 0,
    person_pop_add_view: 0,
    person_pop_add_like: 0,
    person_pop_add_dislike: 0,
    person_pop_add_star: 0
  }
  data.person_pop_add_view = selectFrom(1000, 5000) //总浏览
  const monthView = +(data.person_pop_add_view / 2).toFixed(0) //月浏览
  const weekView = +(data.person_pop_add_view / 8).toFixed(0) //周浏览
  const dayView = +(data.person_pop_add_view / 60).toFixed(0) //日浏览
  data.person_pop_add_like = selectFrom(10, 600) //总点赞
  const monthLike = +(data.person_pop_add_like / 2).toFixed(0) //月点赞
  const weekLike = +(data.person_pop_add_like / 8).toFixed(0) //周点赞
  const dayLike = +(data.person_pop_add_like / 60).toFixed(0) //日点赞
  data.person_pop_add_dislike = 0 //总点踩
  const monthDislike = 0 //月点踩
  const weekDislike = 0 //周点踩
  const dayDislike = 0 //日点踩
  //总人气
  data.person_pop_add_total =
    data.person_pop_add_view + 100 * data.person_pop_add_like + 100 * data.person_pop_add_dislike
  //月人气
  data.person_pop_add_month = monthView + 100 * monthLike + 100 * monthDislike
  //周人气
  data.person_pop_add_week = weekView + 100 * weekLike + 100 * weekDislike
  //日人气
  data.person_pop_add_day = dayView + 100 * dayLike + 100 * dayDislike
  //随机评分，用于计算星级
  const score = selectFrom(6.0, 10.0)
  //星级
  data.person_pop_add_star = floorFunc(score / 2)
  console.log('score: ', score.toFixed(1))
  console.log('person_pop_add_star: ', data.person_pop_add_star.toFixed(1))
  Object.assign(formState, data)
  console.log('formState: ', formState)
}
//开启关闭补偿按钮状态
export const isPopularity = ref(false)
